package com.kingwood.algorithm.test;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串相关的操作题
 */
public class ZiFuChuan {

    /**
     * https://fcqian.blog.csdn.net/article/details/135398703
     * 火星文计算2
     * 解题方式可以有两种：正则匹配 和 栈
     * @param str
     * @return
     */
    public static long getHuoXingWenResult(String str) {
        long ans = 0;
        Pattern p = Pattern.compile("(\\d+)#(\\d+)");

        while (true) {
            Matcher m = p.matcher(str);
            if (!m.find()) break;

            String subStr = m.group(0);
            long x = Long.parseLong(m.group(1));
            long y = Long.parseLong(m.group(2));
            str = str.replaceFirst(subStr, 4 * x + 3 * y + 2 + "");
        }

        Pattern p2 = Pattern.compile("(\\d+)#(\\d+)");
        while (true) {
            Matcher m = p2.matcher(str);
            if (!m.find()) break;

            String subStr = m.group(0);
            Long x = Long.parseLong(m.group(1));
            Long y = Long.parseLong(m.group(2));
            str = str.replaceFirst(subStr, 4 * x + 3 * y + 2 + "");
        }

        // x$y => 2* x + y+3
        // 5$8$10
        ans = Arrays.stream(str.split("\\$")).map(Long::parseLong).reduce((x, y) -> 2 * x + y + 3).orElse(0L);

        long myAns = 0;
        String[] resArr = str.split("\\$");
        if (resArr.length <= 1) {
            myAns = Long.valueOf(resArr[0]);
        } else {
            LinkedList<Long> list = new LinkedList<>();
            for (String item : resArr) {
                list.addLast(Long.valueOf(item));
            }

            while (list.size() > 0) {
                long x = list.removeFirst();
                long y = list.removeFirst();
                long res =  2 * x + y + 3;

                if (list.size() == 0) {
                    myAns = res;
                    break;
                } else {
                    list.addFirst(res);
                }
            }
        }

        System.out.println(myAns);
        return ans;
    }

    /**
     * 英文输入法
     * https://fcqian.blog.csdn.net/article/details/127418419
     * 简单的逻辑题，应该是主要考察数组去重，数组字典排序，过滤等知识
     * @param str
     * @param pre
     * @return
     */
    public static String getStrStartWithPre(String str, String pre) {
        String[] tmp = str.split("[^a-zA-Z]");
        TreeSet<String> cache = new TreeSet<>();
        Collections.addAll(cache, tmp);

        StringJoiner sj = new StringJoiner(" ");
        cache.stream().filter(s -> s.startsWith("pre")).forEach(s -> sj.add(s));

        String ans = sj.toString();
        if (ans.length() > 0) {
            return ans;
        }

        return pre;
    }

    public static String getStrStartWithPre2(String str, String pre) {
        String[] tmp = str.split("[^a-zA-Z]");
        TreeSet<String> cache = new TreeSet<>();
        Collections.addAll(cache, tmp);

        StringJoiner sj = new StringJoiner(" ");
        cache.stream().filter(s -> s.startsWith(pre)).forEach(s -> sj.add(s));

        String ans = sj.toString();
        if (ans.length() > 0) {
            return ans;
        }

        return pre;
    }

    /**
     * 字符串筛选排序
     * https://fcqian.blog.csdn.net/article/details/127944487
     * @param str
     * @param k
     * @return
     */
    public static int getIndexOfKChar(String str, int k) {
        char[] chars = str.toCharArray();
        Arrays.sort(chars);

        System.out.println(Arrays.toString(chars));
        for (int i=0; i<chars.length; i++) {
            int code = chars[i];
            System.out.print(code + " ");
        }
        System.out.println();

        if (k > str.length()) {
            k = str.length();
        }

        char tar = chars[k - 1];
        return str.indexOf(tar);
    }

    public static int getIndexOfKChar2(String str, int k) {
        char[] chars = str.toCharArray();
        Arrays.sort(chars);

        if (k > str.length()) {
            k = str.length();
        }

        char tar = chars[k - 1];
        return str.indexOf(tar);
    }

    /**
     * 连续字母长度
     * https://fcqian.blog.csdn.net/article/details/127238467
     * @param str
     * @param k
     * @return
     */
    public static int getLianXuZiMuLen(String str, int k) {
        int ans = -1;
        if (k <= 0) {
            return ans;
        }

        str += "0";
        int len = str.length();
        HashMap<Character, Integer> countMap = new HashMap<>();

        int subLen = 1;
        char b = str.charAt(0);
        for (int i=1; i<len; i++) {
            char c = str.charAt(i);
            if (b == c) {
                subLen++;
            } else {
                if (!countMap.containsKey(b) || countMap.get(b) < subLen) {
                    countMap.put(b, subLen);
                }
                subLen = 1;
                b = c;
            }
        }

        Integer[] arr = countMap.values().toArray(new Integer[0]);
        if (k > arr.length) {
            return ans;
        }

        Arrays.sort(arr, (x, y) -> y - x);
        ans = arr[k - 1];
        return ans;
    }

    public static int getLianXuZiMuLen2(String str, int k) {
        int len = str.length();
        if (k <= 0 || k > len) {
            return -1;
        }

        Integer[] cnts = new Integer[26];
        Arrays.fill(cnts, 0);
        int ans = 1;
        char pre = str.charAt(0);

        for (int i=1; i<len; i++) {
            char cur = str.charAt(i);
            if (pre == cur) {
                ans++;
            } else {
                if (ans > cnts[pre - 'A']) {
                    cnts[pre - 'A'] = ans;
                }
                pre = cur;
                ans = 1;
            }

            if (i == len - 1) {
                if (ans > cnts[cur - 'A']) {
                    cnts[cur - 'A'] = ans;
                }
            }
        }

        Arrays.sort(cnts, (x, y) -> y - x);
        ans = cnts[k - 1];
        return ans;
    }

    /**
     * 字符串序列判定
     * https://fcqian.blog.csdn.net/article/details/127228559
     * 本题可以利用双指针来解决。
     * @param s
     * @param l
     * @return
     */
    public static int getValidSubStr(String s, String l) {
        int i = 0;
        int j = 0;
        int ans = -1;

        List<Character> charList = new ArrayList<>();
        HashSet<Character> charSet = new HashSet<>();
        for (int k=0; k<s.length(); k++) {
            char c = s.charAt(k);
            if (charSet.contains(c)) {
                continue;
            }
            charSet.add(c);
            charList.add(c);
        }

        while (i < charList.size() && j < l.length()) {
            if (charList.get(i) == l.charAt(j)) {
                i++;
                ans = j;
            }
            j++;
        }

        return ans;
    }

    public static int maxLongYuanYinSubStr(int flaw, String str) {
        int ans = 0;
        char[] yuan = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
        HashSet<Character> set = new HashSet<>();
        for (char c : yuan) {
            set.add(c);
        }

        ArrayList<Integer> idxs = new ArrayList<>();
        for (int i=0; i<str.length(); i++) {
            char c = str.charAt(i);
            if (set.contains(c)) {
                idxs.add(i);
            }
        }

        int n = idxs.size();
        int l = 0;
        int r = 0;

        while (r < n) {
            // 瑕疵度计算公式
            int diff = idxs.get(r) - idxs.get(l) - (r - l);
            if (diff > flaw) {
                l++;
            } else if (diff < flaw) {
                r++;
            } else {
                ans = Math.max(ans, idxs.get(r) - idxs.get(l) + 1);
                r++;
            }
        }

        return ans;
    }

    /**
     * https://fcqian.blog.csdn.net/article/details/127418499
     * 字符串变换最小字符串
     * @param str
     * @return
     */
    public static String getMinStrAfterChange(String str) {
        char[] minSArr = str.toCharArray();
        Arrays.sort(minSArr);

        String minStr = new String(minSArr);
        if (str.equals(minStr)) {
            return str;
        }

        char[] sArr = str.toCharArray();
        for (int i=0; i< str.length(); i++) {
            if (sArr[i] != minSArr[i]) {
                char tmp = sArr[i];
                sArr[i] = minSArr[i];

                int swapIndex = str.lastIndexOf(tmp);
                sArr[swapIndex] = tmp;
                break;
            }
        }

        return new String(sArr);
    }

    public static String getMinStrAfterChange2(String str) {
        char[] minStrArr = str.toCharArray();
        Arrays.sort(minStrArr);

        String minStr = new String(minStrArr);
        if (str.equals(minStr)) {
            return str;
        }

        char[] curStrArr = str.toCharArray();
        int len = curStrArr.length;

        for (int i=0; i<len; i++) {
            char c = curStrArr[i];
            if (c != minStrArr[i]) {
                // 交换
                char tmp = curStrArr[i];
                curStrArr[i] = minStrArr[i];
                int lastIndex = str.lastIndexOf(minStrArr[i]);
                curStrArr[lastIndex] = tmp;
                break;
            }
        }

        return new String(curStrArr);
    }

    /**
     * 拼接URL
     * https://fcqian.blog.csdn.net/article/details/126005113
     * @param str
     * @return
     */
    public static String pinJieUrl(String str) {
        String[] arr = str.replaceAll(" ", "").split(",");
        if (arr.length == 0) {
            return "/";
        }

        if (arr[0].endsWith("/")) {
            if (arr[1].startsWith("/")) {
                return arr[0] + arr[1].substring(1);
            } else {
                return arr[0] + arr[1];
            }
        } else {
            if (arr[1].startsWith("/")) {
                return arr[0] + arr[1];
            } else {
                return arr[0] + "/" + arr[1];
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

//        String str = scanner.nextLine();
//        long ans = getHuoXingWenResult(str);
//        System.out.println(ans);

//        String str = scanner.nextLine();
//        String pre = scanner.nextLine();
//        String res = getStrStartWithPre(str, pre);
//        System.out.println(res);

//        String str = scanner.nextLine();
//        int k = scanner.nextInt();
//        System.out.println(getIndexOfKChar(str, k));

//        String s = scanner.nextLine();
//        String l = scanner.nextLine();
//        System.out.println(getValidSubStr(s, l));

        String str = scanner.nextLine();
//        int k = scanner.nextInt();
//        System.out.println(getLianXuZiMuLen(str, k));
//        System.out.println(getLianXuZiMuLen2(str, k));

        System.out.println(pinJieUrl(str));
    }
}
